<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>最小栈 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        pre code {
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .number { color: #b5cea8; }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="gradient-bg text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
            <div class="mb-6">
                <i class="fas fa-layer-group text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl font-bold mb-4 serif-font">最小栈</h1>
            <p class="text-xl opacity-90 leading-relaxed">
                在常数时间内检索最小元素的栈设计
            </p>
            <div class="mt-8 flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>时间复杂度 O(1)
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-database mr-2"></i>空间复杂度 O(n)
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.2s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <i class="fas fa-question-circle text-3xl text-purple-600 mr-4"></i>
                    <h2 class="text-2xl font-bold">题目描述</h2>
                </div>
                <p class="text-gray-700 leading-relaxed text-lg">
                    设计一个支持 push、pop、top 操作，并能在常数时间内检索到最小元素的栈。要求实现 MinStack 类，支持：
                </p>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-6">
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <i class="fas fa-arrow-down text-purple-600 mr-2"></i>
                        <strong>push(x)</strong>：将元素 x 推入栈中
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <i class="fas fa-arrow-up text-purple-600 mr-2"></i>
                        <strong>pop()</strong>：删除栈顶的元素
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <i class="fas fa-eye text-purple-600 mr-2"></i>
                        <strong>top()</strong>：获取栈顶元素
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <i class="fas fa-compress-arrows-alt text-purple-600 mr-2"></i>
                        <strong>getMin()</strong>：检索栈中的最小元素
                    </div>
                </div>
                <p class="mt-6 text-gray-600 bg-yellow-50 p-4 rounded-lg">
                    <i class="fas fa-exclamation-triangle text-yellow-600 mr-2"></i>
                    <strong>要求</strong>：所有操作必须在常数时间内完成
                </p>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.3s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <i class="fas fa-lightbulb text-3xl text-yellow-500 mr-4"></i>
                    <h2 class="text-2xl font-bold">核心考点</h2>
                </div>
                <div class="flex flex-wrap gap-3">
                    <span class="bg-gradient-to-r from-purple-500 to-pink-500 text-white px-6 py-3 rounded-full font-medium">
                        <i class="fas fa-layer-group mr-2"></i>栈
                    </span>
                    <span class="bg-gradient-to-r from-blue-500 to-cyan-500 text-white px-6 py-3 rounded-full font-medium">
                        <i class="fas fa-clone mr-2"></i>辅助栈
                    </span>
                </div>
            </div>
        </section>

        <!-- Solution Approach -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.4s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <i class="fas fa-brain text-3xl text-green-600 mr-4"></i>
                    <h2 class="text-2xl font-bold">解题思路</h2>
                </div>
                <div class="bg-green-50 p-6 rounded-lg mb-6">
                    <p class="text-lg leading-relaxed">
                        使用两个栈：一个存储数据，另一个存储当前栈的最小值。每次入栈时，若新元素小于等于辅助栈栈顶，则将其入辅助栈；出栈时，若数据栈顶等于辅助栈顶，则辅助栈也出栈。
                    </p>
                </div>
                
                <!-- Visual Diagram -->
                <div class="mermaid">
                    graph TD
                        A[MinStack] --> B[数据栈<br/>存储所有元素]
                        A --> C[辅助栈<br/>存储最小值]
                        B --> D[push: 直接入栈]
                        C --> E[push: 条件入栈<br/>val <= minStack.peek]
                        B --> F[pop: 直接出栈]
                        C --> G[pop: 条件出栈<br/>stack.peek == minStack.peek]
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#4ec9b0,stroke:#333,stroke-width:2px
                        style C fill:#dcdcaa,stroke:#333,stroke-width:2px
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-8">
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <i class="fas fa-clock text-blue-600 text-2xl mb-3"></i>
                        <h3 class="font-bold mb-2">时间复杂度</h3>
                        <p class="text-gray-700">O(1) - 所有操作均为常数时间</p>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <i class="fas fa-memory text-purple-600 text-2xl mb-3"></i>
                        <h3 class="font-bold mb-2">空间复杂度</h3>
                        <p class="text-gray-700">O(n) - 需要额外的辅助栈</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.5s;">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <div class="flex items-center mb-6">
                    <i class="fas fa-code text-3xl text-indigo-600 mr-4"></i>
                    <h2 class="text-2xl font-bold">示例代码</h2>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <div class="code-dot bg-red-500"></div>
                        <div class="code-dot bg-yellow-500"></div>
                        <div class="code-dot bg-green-500"></div>
                        <span class="text-gray-400 ml-4">MinStack.java</span>
                    </div>
                    <pre><code><span class="keyword">class</span> <span class="type">MinStack</span> {
    <span class="comment">// 数据栈，存储所有元素</span>
    <span class="keyword">private</span> <span class="type">Stack</span>&lt;<span class="type">Integer</span>&gt; stack;
    <span class="comment">// 辅助栈，存储当前栈中的最小值</span>
    <span class="keyword">private</span> <span class="type">Stack</span>&lt;<span class="type">Integer</span>&gt; minStack;
    
    <span class="comment">/** 初始化数据结构 */</span>
    <span class="keyword">public</span> <span class="function">MinStack</span>() {
        stack = <span class="keyword">new</span> <span class="type">Stack</span>&lt;&gt;();
        minStack = <span class="keyword">new</span> <span class="type">Stack</span>&lt;&gt;();
    }
    
    <span class="comment">/**
     * 将元素推入栈中
     * @param val 要推入的元素
     */</span>
    <span class="keyword">public</span> <span class="keyword">void</span> <span class="function">push</span>(<span class="keyword">int</span> val) {
        <span class="comment">// 将元素加入数据栈</span>
        stack.<span class="function">push</span>(val);
        
        <span class="comment">// 如果辅助栈为空或当前元素小于等于辅助栈顶元素，将其加入辅助栈</span>
        <span class="keyword">if</span> (minStack.<span class="function">isEmpty</span>() || val &lt;= minStack.<span class="function">peek</span>()) {
            minStack.<span class="function">push</span>(val);
        }
    }
    
    <span class="comment">/** 删除栈顶的元素 */</span>
    <span class="keyword">public</span> <span class="keyword">void</span> <span class="function">pop</span>() {
        <span class="comment">// 如果数据栈顶元素等于辅助栈顶元素，同时从辅助栈移除</span>
        <span class="keyword">if</span> (stack.<span class="function">peek</span>().<span class="function">equals</span>(minStack.<span class="function">peek</span>())) {
            minStack.<span class="function">pop</span>();
        }
        
        <span class="comment">// 从数据栈移除栈顶元素</span>
        stack.<span class="function">pop</span>();
    }
    
    <span class="comment">/**
     * 获取栈顶元素
     * @return 栈顶元素
     */</span>
    <span class="keyword">public</span> <span class="keyword">int</span> <span class="function">top</span>() {
        <span class="comment">// 返回数据栈的栈顶元素</span>
        <span class="keyword">return</span> stack.<span class="function">peek</span>();
    }
    
    <span class="comment">/**
     * 检索栈中的最小元素
     * @return 栈中的最小元